from GraphAbstract.EdgeAbstract.EdgeCtrlPoint import EdgeCtrlPoint;
from PyQt4 import QtGui;

class AddCtrlPtCommand(QtGui.QUndoCommand):
    def __init__(self, gEdge, iNewItem, newP, graphicsScene=None, bAddedFromMouse=True):
        msgUndo = "Add New Edge Control Point";
        super(AddCtrlPtCommand, self).__init__(msgUndo);
        self.graphicsScene = graphicsScene;
        self.newCtrlPt = EdgeCtrlPoint(gEdge, newP, True);
        self.gEdge = gEdge;
        self.bAddedFromMouse = bAddedFromMouse;
        self.iNewCtrlPt = iNewItem;
    
    def undo(self):
        self.gEdge.removeCtrlPt(self.newCtrlPt);
        self.newCtrlPt.setVisible(False);
    
    def redo(self):
        self.gEdge.addCtrlPt(self.iNewCtrlPt, self.newCtrlPt);
        self.newCtrlPt.setVisible(True);


class DeleteCtrlPtCommand(QtGui.QUndoCommand):
    def __init__(self, iNewItem=0, gNewItem=None, graphicsScene=None):
        msgUndo = "Delete Edge Control Point";
        super(DeleteCtrlPtCommand, self).__init__(msgUndo);
        self.graphicsScene = graphicsScene;
        self.gCtrlPt = gNewItem;
        self.gEdge = gNewItem.parentObject();
        self.iCtrlPt = iNewItem;
    
    def undo(self):
        self.gEdge.addCtrlPt(self.iCtrlPt, self.gCtrlPt);
        self.gCtrlPt.setVisible(True);
    
    def redo(self):
        self.gEdge.removeCtrlPt(self.gCtrlPt);
        self.gCtrlPt.setVisible(False);


class MoveCtrlPtCommand(QtGui.QUndoCommand):
    def __init__(self, gSelectedCtrlPts=None):
        self.oldPos = [];
        self.newPos = [];
        self.gSelectedCtrlPts = gSelectedCtrlPts;
        for i in range(len(self.gSelectedCtrlPts)):
            self.oldPos.append(self.gSelectedCtrlPts[i].oldPos);
            self.newPos.append(self.gSelectedCtrlPts[i].newPos);
        if len(gSelectedCtrlPts) > 1:
            msgUndo = "Move Multiple Edge Control Points";
        else:
            msgUndo = "Move Edge Control Point to " + str(self.newPos[0].x()) + \
            ", " + str(self.newPos[0].y());
        super(MoveCtrlPtCommand, self).__init__(msgUndo);
    
    def undo(self):
        for i in range(len(self.gSelectedCtrlPts)):
            self.gSelectedCtrlPts[i].setPos(self.oldPos[i]);
    
    def redo(self):
        for i in range(len(self.gSelectedCtrlPts)):
            self.gSelectedCtrlPts[i].setPos(self.newPos[i]);
